/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  // const valid_map = ['admin', 'editor']
  return valid_map.indexOf(str.trim()) >= 0
}

//验证新用户搜索手机号是否为数字
export function checkPhoneNumber(phoneNumber){
  let phone = /^[0-9]{8,12}$/;
  if(phone.test(phoneNumber) === true){
    return true;
  }else{
    return false;
  }
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url) {
  const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str) {
  const reg = /^[a-z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str) {
  const reg = /^[A-Z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str) {
  const reg = /^[A-Za-z]+$/
  return reg.test(str)
}

/**
 * 验证邮箱
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email) {
  const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  // const reg = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
  return reg.test(email)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str) {
  if (typeof str === 'string' || str instanceof String) {
    return true
  }
  return false
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg) {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}

/**
 * 验证密码
 * @param {string} email
 * @returns {Boolean}
 */
export function validPassword(password){
  const reg = /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])[a-zA-Z0-9]+$/;
  return reg.test(password);
}

/**
 * 格式化时间
 */
export function formatHour(time){
  let formatTime = new Date(time);
  let year = formatTime.getFullYear();
  let month = formatTime.getMonth()+1;
  let date = formatTime.getDate();
  
  return year+'-'+month+'-'+date;
}

/**
 * 格式化给后端传的时间
 */
export function formatDate(time){
  let formatTime = new Date(time);
  let year = formatTime.getFullYear();
  let month = formatTime.getMonth()+1;
  let date = formatTime.getDate();
  let hours = formatTime.getHours();
  let minutes = formatTime.getMinutes();
  let second = formatTime.getSeconds();

  return year +'-'+ toDub(month) +'-'+ toDub(date) +' '+ toDub(hours) +':'+ toDub(minutes) +':'+ toDub(second);
}

/**
 * 格式化录音时间
 */
export function formatTime(second){
  if(second){
    let hours = Math.floor(second/3600);
    second = second - hours * 3600;
    let minutes = Math.floor(second/60);
    second = second - minutes * 60;

    return toDub(hours)+':'+toDub(minutes)+':'+toDub(second);
  }else{
    return '00:00:00';
  }
}

/**
 * 处理时间，小于10补0
 */
export function toDub(num){
  if(num < 10){
    return '0' + num;
  }else{
    return num;
  }
}
